package org.luxor.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.luxor.commons.core.utils.BeanUtils;
import org.luxor.system.entity.SysMenuEntity;
import org.luxor.system.entity.vo.router.MenuVo;
import org.luxor.system.mapper.SysMenuMapper;
import org.luxor.system.service.ISysMenuService;
import org.luxor.system.transform.MenuTransform;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 菜单管理
 *
 * @author Mark @date 2020-07-26 16:52:22
 */
@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenuEntity> implements ISysMenuService {

    @Override
    public ArrayList<MenuVo> list(Long parentId) {
        List<SysMenuEntity> rootList = super.list(Wrappers.lambdaQuery(SysMenuEntity.class)
                .eq(SysMenuEntity::getParentId, parentId)
                .eq(SysMenuEntity::getDeleted, false)
                .orderByAsc(SysMenuEntity::getOrderNo));
        // 组装菜单VO
        ArrayList<MenuVo> menuVoList = new ArrayList<>();
        for (SysMenuEntity menu : rootList) {
            ArrayList<MenuVo> children = list(menu.getId());
            menuVoList.add(MenuTransform.toMenuVo(menu, children));
        }
        return menuVoList;
    }

    @Override
    public IPage<SysMenuEntity> page(IPage<SysMenuEntity> page, Map query) {
        SysMenuEntity queryEntity = BeanUtils.toObject(SysMenuEntity.class, query);
        return super.page(page, Wrappers.query(queryEntity));
    }

}
